package UniqueInfoboxAttributes;

import java.io.*;
import zclTool.*;

import java.util.*;

class InfoAtt {
	String info_name;
	String attribute;

	InfoAtt(String info_name, String attribute) {
		this.info_name = info_name;
		this.attribute = attribute;
	}
}

public class Map2FeiResult {

	/**
	 * This code is to map Raphael's 5000+ attributes into fei's schemamapping
	 * to find out
	 * /projects/pardosa/s2/wufei/kylin/dataset/wikipedia/KOGForPublic
	 * */

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// how many overlap features
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"temp"));
		HashMap<String, Integer> att_size = new HashMap<String, Integer>();
		BufferedReader br = new BufferedReader(new FileReader(
				"attributeNames.full.sort.uniq"));
		String aline;
		while ((aline = br.readLine()) != null) {
			aline = aline.trim();
			int start = aline.indexOf(" ");
			int size = Integer.parseInt(aline.substring(0, start));
			String att = aline.substring(start).replace("@", "/").trim();
			att_size.put(att.toLowerCase(), size);
		}
		br.close();

		DelimitedReader dr = new DelimitedReader("schemaMapping");
		HashSet<String> att_fei = new HashSet<String>();
		// HashSet<String>att_rph = new HashSet<String>();
		String[] line;

		while ((line = dr.read()) != null) {
			String a = (line[0] + "/" + line[2]).toLowerCase();
			String b = (line[1]+"/"+line[3]).toLowerCase();
			att_fei.add(a);
			att_fei.add(b);
		}
		dr.close();
		
		Iterator<String>it = att_fei.iterator();
		int homanyDispper = 0;
		while(it.hasNext()){
			String a = it.next();
			if (att_size.containsKey(a)) {
				//System.out.println(a + "\t" + att_size.get(a));
				osw.write(a + "\t" + att_size.get(a)+"\n");
			} else {
				System.out.println(a);
				homanyDispper++;
			}
		}
		osw.close();
		System.out.println(homanyDispper);
		// int howmany = 0;
		// dr = new DelimitedReader("atts.text.merged-good");
		// while ((line = dr.read()) != null) {
		// String a = line[1].toLowerCase();
		// if(att_fei.contains(a)){
		// howmany++;
		// System.out.println(a);
		// }
		// }
		// System.out.println(howmany);
		// dr.close();
	}

	public static void main2(String[] args) throws Exception {
		// TODO Auto-generated method stub
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"rph_mapping"));
		DelimitedReader dr = new DelimitedReader("schemaMapping");
		List<String> equals_list = new ArrayList<String>();
		HashMap<String, String> equals_namemap = new HashMap<String, String>();
		List<InfoAtt> list = new ArrayList<InfoAtt>();

		StrCmp sc = new StrCmp(true);

		String[] line;
		while ((line = dr.read()) != null) {
			String a = (line[0] + "/" + line[2]);
			String b = (line[1] + "/" + line[3]);
			String a0 = line[2].toLowerCase();
			String b0 = line[3].toLowerCase();
			String putToList = a + "\t" + b;
			equals_list.add(putToList);
			String putToHash = a0 + "\t" + b0;
			equals_namemap.put(putToHash, putToList);
		}
		dr.close();

		dr = new DelimitedReader("atts.text.merged-good");
		while ((line = dr.read()) != null) {
			String a = line[1];
			String b[] = a.split("/");
			if (b.length < 2) {
				break;
			}
			InfoAtt ia = new InfoAtt(b[0], b[1]);
			list.add(ia);
		}
		dr.close();

		for (int i = 0; i < list.size(); i++) {
			for (int j = i + 1; j < list.size(); j++) {
				String a = list.get(i).attribute;
				String b = list.get(j).attribute;
				String show = list.get(i).info_name + "/"
						+ list.get(i).attribute + "\t" + list.get(j).info_name
						+ "/" + list.get(j).attribute;
				String checkEqual = a + "\t" + b;
				String checkEqual2 = b + "\t" + a;
				if (equals_namemap.containsKey(checkEqual)
						|| equals_namemap.containsKey(checkEqual2)) {
					String a1 = equals_namemap.get(checkEqual);
					if (a1 == null) {
						a1 = equals_namemap.get(checkEqual2);
					}

					double rat = sc.sharewordsRatio(show, a1);
					if (rat > 0.9) {
						System.out.println(show + "\t\t" + a1);
						osw.write(show + "\n");
					}
				}

			}
		}
		osw.close();
	}

}
